import logging
import unittest

import requests
from parameterized import parameterized

import utils
from api.Recharge_api import RechargeApi
from api.reg_login_api import RegLoginApi


class TestRecharge(unittest.TestCase):

    def setUp(self) -> None:

        # 创建session对象
        self.session =requests.session()
        # 实例化登录模块封装的类
        self.reg_login_api=RegLoginApi(self.session)
        # 调用登录接口，完成用户登录
        self.reg_login_api.user_login("13148939905","123456")
        # 调用封装的充值模块类
        self.recharge_api = RechargeApi(self.session)

    def tearDown(self) -> None:
            # 关闭session对象
        self.session.close()
    # 获取充值验证码测试用例
    @parameterized.expand(utils.read_json("recharge_data","recharge_verify_code"))
    def test01_get_recharge_verify_code(self,r, exp_status_code):
        # 获取充值图片验证码
        res =self.recharge_api.get_recharge_verify_code(r)
        logging.info("获取图片验证码响应状态码{}".format(res.status_code))
        logging.info("获取图片验证码的接口的url{}".format(res.url))

        # 断言
        self.assertEqual( exp_status_code,res.status_code)

    # 充值测试用例
    @parameterized.expand(utils.read_json("recharge_data","recharge"))
    def test02_recharge(self,amount,valicode,exp_status_code, exp_status, exp_description):
        self.recharge_api.get_recharge_verify_code(0.123123)
        # 获取充值图片验证码
        res=self.recharge_api.recharge(amount,valicode)
        res_dict= res.json()
        logging.info("获取充值接口响应体数据:{}".format(res_dict))
        # 充值
        # 断言
        utils.common_assert(self,res,exp_status_code, exp_status, exp_description)



    # 第三方充值测试用例
    def test03_third_recharge(self):
        # 获取充值图片验证码
        self.recharge_api.get_recharge_verify_code(0.123123)
        # 充值
        res_recharge=self.recharge_api.recharge("10000","8888")
        # 获取第三方开户接口的url
        url=utils.html_util(res_recharge)[0]
        # 获取第三方开户接口的请求体数据
        data_dict =utils.html_util(res_recharge)[1]
        # 获取第三方接口返回的响应结果
        res3_recharge = self.recharge_api.third_recharge(url, data_dict)
        logging.info("第三方充值接口url:{}".format(url))
        logging.info("第三方充值返回的响应数据:{}".format(res3_recharge.text))
        # 断言
        self.assertIn("OK",res3_recharge.text)


